package com.wephone.Student.ui;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.wephone.Student.R;
import com.wephone.Student.bean.data.MyLocationInfo;
import com.wephone.Student.bean.data.PurchaseInfoData;
import com.wephone.Student.huanxin.activity.ChatActivity;
import com.wephone.Student.util.Constants;

public class GlobalPurchasingDetailActivity extends BaseActivity {
	public static PurchaseInfoData data;
	private TextView detailTitle;
	private TextView detailText1;
	private TextView detailText2;
	private TextView detailText3;
	private TextView detailText4;
	private ImageView itemImage;
	private TextView userText1;
	private TextView userText2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_purchasing_detail);
		
		init();
	}
	
	private void init(){
		detailTitle = (TextView)findViewById(R.id.detail_title);
		detailText1 = (TextView)findViewById(R.id.detail_text_1);
		detailText2 = (TextView)findViewById(R.id.detail_text_2);
		detailText3 = (TextView)findViewById(R.id.detail_text_3);
		detailText4 = (TextView)findViewById(R.id.detail_text_4);
		itemImage = (ImageView)findViewById(R.id.item_image);
		userText1 = (TextView)findViewById(R.id.user_text_1);
		userText2 = (TextView)findViewById(R.id.user_text_2);
		
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		detailTitle.setText(data.getpTitle());
		if(!TextUtils.isEmpty(data.getuInfo().getuLocDetail())){
			Gson gson = new Gson();
			MyLocationInfo locInfo = gson.fromJson(data.getuInfo().getuLocDetail(), MyLocationInfo.class);
			if(locInfo.getType().equals("Foreign")){
				userText1.setText(locInfo.getSchool());
				userText2.setText(locInfo.getCountry());
			}else if(locInfo.getType().equals("Returnees")){
				userText1.setText(locInfo.getSchool1());
				userText2.setText(locInfo.getCountry());
			}else if(locInfo.getType().equals("PlanGoOut")){
				userText1.setText(locInfo.getToschool());
				userText2.setText(locInfo.getCountry());
			}
		}
		detailText1.setText(data.getpFrom());
		detailText2.setText(data.getpTo());
		try {
			Date date = new Date(Long.parseLong(data.getpTs())*1000);
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
			detailText3.setText(sdf2.format(date));
		} catch (Exception e) {
			e.printStackTrace();
		}
		detailText4.setText(data.getpDetail());
		if(!TextUtils.isEmpty(data.getuInfo().getuIcon())){
			DisplayImageOptions options = new DisplayImageOptions.Builder()
	        .showStubImage(R.drawable.default_user_logo)
	        .showImageOnFail(R.drawable.default_user_logo)
	        .bitmapConfig(Bitmap.Config.ARGB_8888)
	        .cacheInMemory()
	        .cacheOnDisc()
	        .displayer(new RoundedBitmapDisplayer(10))
	        .build();
//			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getuInfo().getuIcon(), itemImage, options);
			String endStr = data.getuInfo().getuIcon().substring(data.getuInfo().getuIcon().lastIndexOf("."));
			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getuInfo().getuIcon().replace(endStr, "_150_150"+endStr), itemImage, options);
		}
		itemImage.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Bundle bundle = new Bundle();
				bundle.putSerializable("userData", data.getuInfo());
				startActivity(new Intent(getApplicationContext(),UserInfoDetailActivity.class).putExtras(bundle));
			}
		});
		
		findViewById(R.id.btn_bottom_call).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "抱歉，该用户没有留下联系方式", Toast.LENGTH_SHORT).show();
			}
		});
		findViewById(R.id.btn_bottom_chat).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				MyMessageActivity.toUserId = data.getuInfo().getuId();
//				MyMessageActivity.toUserName = data.getuInfo().getuName();
//				MyMessageActivity.toUserInfo = data.getuInfo();
//				startActivity(new Intent(getApplicationContext(),MyMessageActivity.class));
				
				ChatActivity.toUserInfo = data.getuInfo();
				Intent intent = new Intent(getApplicationContext(),ChatActivity.class);
				intent.putExtra("chatType", ChatActivity.CHATTYPE_SINGLE);
				intent.putExtra("userId", data.getuInfo().getuId());
				startActivity(intent);
			}
		});
		
		if(BaseActivity.getMyInfo(getApplicationContext()).getuId().equals(data.getuInfo().getuId())){
			findViewById(R.id.bottom_button_layout).setVisibility(View.GONE);
		}else{
			findViewById(R.id.bottom_button_layout).setVisibility(View.VISIBLE);
		}
	}

}
